package com.qumitech.visualnovel.service.pay.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.pay.*;
import com.qumitech.visualnovel.service.account.AccountService;
import com.qumitech.visualnovel.service.pay.PayService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private AccountService accountService;

    private final String payAdminUrl = "/admin/pay";
    private final String payAccountAdminUrl = "/account";

    @Override
    public BaseRsp listUserPayAccountAdmin(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + payAdminUrl + payAccountAdminUrl + "/list";
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp listPayBlackUserAdmin(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + payAdminUrl + payAccountAdminUrl + "/black/list";
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp addPayBlackUserAdmin(PayBlackUserAdminAddDTO adminAddDTO) {
        adminAddDTO.setOperator(accountService.getOperatorName());
        String url = UrlConstant.SERVER_URL + payAdminUrl + payAccountAdminUrl + "/black";
        String req = JSONObject.toJSONString(adminAddDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp updatePayBlackUserAdmin(PayBlackUserAdminUpdateDTO adminUpdateDTO) {
        adminUpdateDTO.setOperator(accountService.getOperatorName());
        String url = UrlConstant.SERVER_URL + payAdminUrl + payAccountAdminUrl+ "/black";
        String req = JSONObject.toJSONString(adminUpdateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp listPayType() {

        String url = UrlConstant.SERVER_URL + payAdminUrl + "/type/list";
        String body = HttpInvokeUtil.httpGet(url, null, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp listPayPlan(PayPlanAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + payAdminUrl + "/plan/list";
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp savePayPlan(PayPlanAdminDTO adminDTO) {
        adminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL +  payAdminUrl + "/plan";
        String req = JSONObject.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updatePayPlan(PayPlanAdminDTO adminDTO) {
        adminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL +  payAdminUrl + "/plan/" + adminDTO.getPlanId();
        String req = JSONObject.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp rmPayPlan(PayPlanAdminDTO adminDTO) {
        adminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL + payAdminUrl + "/plan/" + adminDTO.getPlanId();
        String req = JSONObject.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listPayApplication(PayApplicationAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + payAdminUrl + "/application/list";
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp savePayApplication(PayApplicationAdminDTO adminDTO) {
        adminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL +  payAdminUrl + "/application";
        String req = JSONObject.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updatePayApplication(PayApplicationAdminDTO adminDTO) {
        adminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL +  payAdminUrl + "/application/" + adminDTO.getApplicationId();
        String req = JSONObject.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp rmPayApplication(PayApplicationAdminDTO adminDTO) {
        adminDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL + payAdminUrl + "/application/" + adminDTO.getApplicationId();
        String req = JSONObject.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    private String getActiveUserName(){
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }

}
